/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.jQgrid;



import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
/**
 *
 * @author CHANWUT
 */
public class JSONUtil {
    
  //  private static final Log logger = LogFactory.getLog(JSONUtil.class);

    public static String convertToJSON(Object obj) throws Exception {
        String json = null;
        try{
        Gson gson = new Gson();
        json = (gson.toJson(obj));
    } catch (Exception ex) {
   	 	ex.printStackTrace();
   	 	throw new Exception ("Display error");
       // System.out.println("Error message" + ex.getMessage());
    }
        //logger.debug(json);
        return json;
    }
    
    public static JsonArray convertStringJsonToJSONArray(String obj) throws Exception {
   	 JsonParser parser = new JsonParser();
   	 JsonElement jsonElement ;
   	 JsonArray jsonArray = null;
        try{
          	 jsonElement = parser.parse(obj);
          	 jsonArray = jsonElement.getAsJsonArray();
    } catch (Exception ex) {
    	ex.printStackTrace();
    	throw new Exception ("Display error");
   	 	//return null;
        
    } finally {
    	parser = null;
    	jsonElement = null;
    }
        return jsonArray;
    }
}
